package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.pojo.Item;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;

	@Override
	public EasyUITable findItemByPage(Integer page, Integer rows) {

		IPage iPage= new Page(page,rows);
		QueryWrapper <Item> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("updated");
		iPage = itemMapper.selectPage(iPage, queryWrapper);
		long total = iPage.getTotal();
		List<Item> itemList=iPage.getRecords();//获取当前页的记录
		return new EasyUITable(total,itemList);
	}

	@Transactional //标记方式使用事务控制
	@Override
	public void saveItem(Item item){
		item.setStatus(1)
				.setCreated(new Date())
				.setUpdated(item.getCreated());
		itemMapper.insert(item);

	}


//	@Override
//	public EasyUITable findItemByPage(Integer page,Integer rows ){
//
//
//		long total = itemMapper.selectCount(null);
//
//		int startIndex = (page-1) *rows;
//
//		List<Item> pageList = itemMapper.findItemByPage(startIndex, rows);
//
//		return new EasyUITable(total,pageList);
//	}
	
}
